﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Configuration;
using System.Text;

public partial class FXFWDTrade : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            GridView1.Visible = false;
    }
    protected void queryBtn_Click(object sender, EventArgs e)
    {
        using (TR_DBDataContext db = new TR_DBDataContext())
        {
            // 取得ODS交易資料
            using (OracleConnection conn = new OracleConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString_ODS"].ConnectionString;
                conn.Open();
                OracleCommand cmd = conn.CreateCommand();
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT * FROM OD_OB_FXFWD WHERE TRDSTATUS='VER' AND SCHEMA='NDF' ");
                sb.Append("AND DATADT=TO_DATE('" + reportDate.Text + "','yyyy/mm/dd')");
                string sqlStr = sb.ToString();
                cmd.CommandText = sqlStr;

                // Read oracle data
                OracleDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    DateTime tradeDate = reader["DATADT"] != DBNull.Value ? (DateTime)reader["DATADT"] : DateTime.Now;
                    string tradeID = reader["TRDID"] != DBNull.Value ? ((string)reader["TRDID"]).TrimEnd().ToString() : "";
                    var rptData = from P in db.TR_Format_A where P.TradeDate.Date.Equals(tradeDate) && P.SequenceNo.Trim().Equals(tradeID) select P;
                    
                    TR_Format_A formatA;
                    if (rptData.Count() > 0)
                        formatA = (from P in db.TR_Format_A where P.TradeDate.Date.Equals(tradeDate) && P.SequenceNo.Trim().Equals(tradeID) select P).First();
                    else
                        formatA = new TR_Format_A();

                    // Table 欄位資料設定
                    formatA.ProductType = '1';
                    formatA.SequenceNo = tradeID;
                    formatA.BankCode = "8050000";
                    formatA.BankType = '1';
                    switch (reader["COMPANY"] != DBNull.Value ? ((string)reader["COMPANY"]).TrimEnd().ToString() : "")
                    {
                        case "DBU":
                            formatA.TransUnit = '1';
                            break;
                        case "OBU":
                            formatA.TransUnit = '2';
                            break;
                        case "HK":
                            formatA.TransUnit = '3';
                            break;
                        case "":
                            formatA.TransUnit = '0';
                            break;
                    }
                    Customer customer = new Customer();
                    customer.customerID = reader["CUST"] != DBNull.Value ? ((string)reader["CUST"]).TrimEnd().ToString() : "";
                    customer.getCustDataFromODS();
                    ItemList itemList = new ItemList();
                    itemList.itemCode = customer.groupID;
                    itemList.itemType = "CptyType";
                    itemList.getItemFromItemList();
                    formatA.CptyType = Convert.ToChar(itemList.itemName);
                    //formatA.CptyType = (char)itemList.itemName;
                    formatA.CptyName = customer.fullName;
                    switch(formatA.CptyType){
                        case '1':
                            BankCodeMaping bankMaping = new BankCodeMaping();
                            bankMaping.summitCustID = reader["CUST"] != DBNull.Value ? ((string)reader["CUST"]).TrimEnd().ToString() : "";
                            bankMaping.getBankCodeMapBySummitID();
                            formatA.CptyCode = bankMaping.fiscBankCode;
                            break;
                        case '2':
                        case '3':
                            formatA.CptyCode = customer.unino.Trim();
                            break;
                        case '4':
                        case '5':
                        case '6':
                        default:
                            formatA.CptyCode = "";
                            break;
                    }
                    formatA.TransReason = '5';
                    formatA.TradeDate = reader["TRDDT"] != DBNull.Value ? (DateTime)reader["DATADT"] : DateTime.MaxValue;
                    formatA.EffectiveDate = reader["TRDDT"] != DBNull.Value ? (DateTime)reader["DATADT"] : DateTime.MaxValue;
                    formatA.MaturityDate = reader["VALDATE"] != DBNull.Value ? (DateTime)reader["VALDATE"] : DateTime.MaxValue;
                    formatA.BuyCcy = reader["BCCY"] != DBNull.Value ? ((string)reader["BCCY"]).TrimEnd().ToString() : "";
                    formatA.BuyNotional = reader["BAMT"] != DBNull.Value ? Convert.ToDecimal(reader["BAMT"]) : Convert.ToDecimal("0.00");
                    formatA.SellCcy = reader["SCCY"] != DBNull.Value ? ((string)reader["SCCY"]).TrimEnd().ToString() : "";
                    formatA.SellNotional = reader["SAMT"] != DBNull.Value ? Convert.ToDecimal(reader["SAMT"]) : Convert.ToDecimal("0.00");
                    formatA.CcyPair = formatA.BuyCcy + "/" + formatA.SellCcy;
                    formatA.NDFSettleCcy = "USD";
                    decimal xRate = reader["XRATE"] != DBNull.Value ? Convert.ToDecimal(reader["XRATE"]) : Convert.ToDecimal("0.00");
                    decimal spotRate = reader["SPOTRATE"] != DBNull.Value ? Convert.ToDecimal(reader["SPOTRATE"]) : Convert.ToDecimal("0.00");
                    formatA.SwapPoint = xRate - spotRate;

                    if (rptData.Count() <= 0)
                        db.TR_Format_A.InsertOnSubmit(formatA);
                    db.SubmitChanges();
                }// end of while()
            }// end of using()
        }// end of using()

        // 顯示報表資料
        displayMode();
    }
    private void displayMode()
    {
        GridView1.Visible = true;
    }
    protected void exportToCsv_Click(object sender, EventArgs e)
    {
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;
        
    }
}
